package cn.com.pagoda.ideplugin.listener;

import cn.com.pagoda.ideplugin.common.transcoder.TranscoderTypeEnum;
import cn.com.pagoda.ideplugin.ui.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author xieluxin
 * @Date 2023/7/7 21:43
 * @Version 1.0
 */
public class TranscoderActionListener implements ActionListener {


    private final TranscoderPanel panel;

    public TranscoderActionListener(TranscoderPanel panel) {
        this.panel = panel;
    }


    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == panel.getHashCalculateBtn()) {
            buttonEnable(panel.getHashCalculateBtn());
            panel.getSubPageCardLayout().show(panel.getSubPagePanel(), TranscoderTypeEnum.HASH_CALCULATE_SUB_PANEL.getCode());
        } else if (event.getSource() == panel.getEncryptAndDecryptBtn()) {
            buttonEnable(panel.getEncryptAndDecryptBtn());
            panel.getSubPageCardLayout().show(panel.getSubPagePanel(), TranscoderTypeEnum.ENCRYPT_AND_DECRYPT_SUB_PANEL.getCode());
        } else if (event.getSource() == panel.getEncodeAndDecodeBtn()) {
            buttonEnable(panel.getEncodeAndDecodeBtn());
            panel.getSubPageCardLayout().show(panel.getSubPagePanel(), TranscoderTypeEnum.ENCODE_AND_DECODE_SUB_PANEL.getCode());
        }
    }

    private void buttonEnable(JButton btn) {
        panel.getEncodeAndDecodeBtn().setEnabled(!panel.getEncodeAndDecodeBtn().equals(btn));
        panel.getEncryptAndDecryptBtn().setEnabled(!panel.getEncryptAndDecryptBtn().equals(btn));
        panel.getHashCalculateBtn().setEnabled(!panel.getHashCalculateBtn().equals(btn));
    }
}
